package com.xzz.finger;

/**
 * @author: hhz
 * @create: 2022-02-21 10:21
 * 剑指 Offer 18. 删除链表的节点
 **/
public class DeleteNode {
    public static void main(String[] args) {
        ListNode n1 = new ListNode(4);
        ListNode n2 = new ListNode(5);
        ListNode n3 = new ListNode(1);
        ListNode n4 = new ListNode(9);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        System.out.println(deleteNode(n1, 0));
    }

    public static ListNode deleteNode(ListNode head, int val) {
        ListNode tail = new ListNode(0);
        tail.next = head;
        ListNode temp = tail;
        while (temp.next!=null){
            if(temp.next.val == val){
                temp.next = temp.next.next;
                break;
            }
            temp = temp.next;
        }
        return tail.next;
    }
}
